<?php
namespace app\home\controller\account;
use app\home\controller\Base;

class Api extends Base
{

	/**
	 * 账户货币查询接口
	 * @return [type] [description]
	 */
	public function coin(){
		$params = $this->request->param();

		$coinId = isset($params['id'])?$params['id']:null;

		$data = model('member_coin')->getAccountByUid(UID,$coinId);
		return json($data);
	}

	/**
	 * 账户货币查询接口
	 * @return [type] [description]
	 */
	public function coin_tag(){
		$params = $this->request->param();

		$coinTag = isset($params['coin'])?$params['coin']:'c_jiangjin';

		$coinId = model('coin')::tagCoin($coinTag)['id'];

		$data = model('member_coin')->where(['uid'=>UID,'coin_id'=>$coinId])->find();
		return json($data);
	}

	/**
	 * 资产变动记录接口
	 * @return [type] [description]
	 */
	public function record(){
		$params = $this->request->param();
        
        $perpagenum = isset($params['limit'])?intval($params['limit']):15;
        $perpagenum = $perpagenum>100?100:$perpagenum;
        $page = isset($params['page'])?intval($params['page']):1;
        $page = $page<1?1:$page;

        unset($params['limit']);
        unset($params['page']);
        $where = [];
        if(isset($params['key'])){
            $where= parseWhere($params['key']);
        }
        $where['uid'] = UID;

        $data = model('member_coin_record')->getPageDatas($where,$page,$perpagenum);
        $data['code'] = 1;
        return json($data);
	}

	public function record_transfer(){
		$params = $this->request->param();
        
        $perpagenum = isset($params['limit'])?intval($params['limit']):15;
        $perpagenum = $perpagenum>100?100:$perpagenum;
        $page = isset($params['page'])?intval($params['page']):1;
        $page = $page<1?1:$page;

        unset($params['limit']);
        unset($params['page']);
        $where = [];
        if(isset($params['key'])){
            $where= parseWhere($params['key']);
        }
        $where['uid'] = UID;

        $data = model('operate_transfer')->getPageDatas($where,$page,$perpagenum);
        $data['code'] = 1;
        return json($data);
	}

	public function record_convert(){
		$params = $this->request->param();
        
        $perpagenum = isset($params['limit'])?intval($params['limit']):15;
        $perpagenum = $perpagenum>100?100:$perpagenum;
        $page = isset($params['page'])?intval($params['page']):1;
        $page = $page<1?1:$page;

        unset($params['limit']);
        unset($params['page']);
        $where = [];
        if(isset($params['key'])){
            $where= parseWhere($params['key']);
        }
        $where['uid'] = UID;

        $data = model('operate_convert')->getPageDatas($where,$page,$perpagenum);
        $data['code'] = 1;
        return json($data);
	}

	public function record_charge(){
		$params = $this->request->param();
        
        $perpagenum = isset($params['limit'])?intval($params['limit']):15;
        $perpagenum = $perpagenum>100?100:$perpagenum;
        $page = isset($params['page'])?intval($params['page']):1;
        $page = $page<1?1:$page;

        unset($params['limit']);
        unset($params['page']);
        $where = [];
        if(isset($params['key'])){
            $where= parseWhere($params['key']);
        }
        $where['uid'] = UID;

        $data = model('operate_charge')->getPageDatas($where,$page,$perpagenum);
        $data['code'] = 1;
        return json($data);
	}

	public function record_withdraw(){
		$params = $this->request->param();
        
        $perpagenum = isset($params['limit'])?intval($params['limit']):15;
        $perpagenum = $perpagenum>100?100:$perpagenum;
        $page = isset($params['page'])?intval($params['page']):1;
        $page = $page<1?1:$page;

        unset($params['limit']);
        unset($params['page']);
        $where = [];
        if(isset($params['key'])){
            $where= parseWhere($params['key']);
        }
        $where['uid'] = UID;

        $data = model('operate_withdraw')->getPageDatas($where,$page,$perpagenum);
        $data['code'] = 1;
        return json($data);
	}


	/**
	 * 转账货币配置接口
	 * @return [type] [description]
	 */
	public function transfer_coin(){

		$data = model('coin_config')::coinConfigByType('transfer');
		$fields = ['coin_id','coin_name','type'];
		foreach($data as &$value):
			$v = [];
			foreach($fields as $field):
				$v[$field] = $value[$field];
			endforeach;
			$value = $v;
		endforeach;
		return json($data);

	}

	/**
	 * 充值货币配置接口
	 * @return [type] [description]
	 */
	public function charge_coin(){

		$data = model('coin_config')::coinConfigByType('charge');
		$fields = ['coin_id','coin_name','type'];
		foreach($data as &$value):
			$v = [];
			foreach($fields as $field):
				$v[$field] = $value[$field];
			endforeach;
			$value = $v;
		endforeach;
		return json($data);

	}

	/**
	 * 提现货币配置接口
	 * @return [type] [description]
	 */
	public function withdraw_coin(){

		$data = model('coin_config')::coinConfigByType('withdraw');
		$fields = ['coin_id','coin_name','type'];
		foreach($data as &$value):
			$v = [];
			foreach($fields as $field):
				$v[$field] = $value[$field];
			endforeach;
			$value = $v;
		endforeach;
		return json($data);

	}

	/**
	 * 转换货币配置接口
	 * @return [type] [description]
	 */
	public function convert_coin(){
		$params = $this->request->param();
		$coinId = isset($params['coin_id'])?$params['coin_id']:null;

		$data = model('coin_config')::coinConfigByType('convert');
		$fields = ['coin_id','coin_name','type','convert_coin_id','convert_coin_name'];
		foreach($data as &$value):
			$v = [];
			foreach($fields as $field):
				$v[$field] = $value[$field];
			endforeach;
			$value = $v;
		endforeach;

		$array = [];
		foreach($data as $value):
			$array[$value['coin_id']]['coin_id']=$value['coin_id'];
			$array[$value['coin_id']]['coin_name']=$value['coin_name'];
			$array[$value['coin_id']]['target'][]=$value;
		endforeach;

		if(!is_null($coinId)){
			return json($array[$coinId]);
		}else{
			return json($array);
		}

	}

}